home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Programmierung
/
Power-Programmierung (Tewi)(1994).iso
/
magazine
/
drdobbs
/
1987
/
08
/
holub.exp
< prev
next >
Wrap
Text File
|
1987-08-12
|
6KB
|
167 lines
1: call( first )
2: int first;
3: {
4: int *argp;
5:
6: argp = &first ;
7: while( *argp != 0 )
8: printf("%d\n", *argp++ );
9: }
Example 1: The call() subroutine
01: struct the_first
02: {
03: int one;
04: double two;
05: };
06:
07: struct the_second
08: {
09: double one;
10: double two;
11: };
12:
13: nannuck( struct_type, struct_itself )
14: int struct_type;
15: {
16: struct the_first *firstp;
17: struct the_second *secondp;
18:
19: if( struct_type == 1 )
20: {
21: firstp = (struct the_first *) &struct_itself;
22: printf("%d, %f\n", firstp->one,
23: firstp->two );
24: }
25: else
26: {
27: secondp = (struct the_second *) &struct_itself;
28: printf("%f, %f\n", secondp->one,
29: secondp->two );
30: }
31: }
32:
33: main()
34: {
35: nannuck(1, 10, 20.0 );
36: nannuck(2, 30.0, 40.0 );
37: }
Example 2:
01: #include <stdio.h>
02: #define va_arg(argp,type) ((type *)(argp += sizeof(type)))[-1]
03:
04: fang( format, args )
05: char *format;
06: {
07: char *argp = (char *) &args;
08:
09: for(; *format ; format++ )
10: {
11: if( *format != '%' )
12: putchar( *format );
13: else
14: {
15: switch( *++format )
16: {
17: case 'c': printf("%c", va_arg(argp,int ) ); break;
18: case 'd': printf("%d", va_arg(argp,int ) ); break;
19: case 's': printf("%s", va_arg(argp,char *) ); break;
20: case 'f': printf("%f", va_arg(argp,double) ); break;
21: }
22: }
23: }
24: }
25:
26: main()
27: {
28: fang("%c, %d, %s, %f\n", '1', 2, "3", 4.5 );
29: }
Example 3:A printf()-like subroutine
01: #include <stdarg.h> /* ANSI */
02:
03: ansi_fang( format )
04: char *format;
05: {
06: va_list argp;
07: va_start( argp, format );
08:
09: for(; *format ; format++ )
10: {
11: if( *format != '%' )
12: putchar( *format );
13: else
14: {
15: switch( *++format )
16: {
17: case 'c': printf("%c", va_arg(argp,int ) ); break;
18: case 'd': printf("%d", va_arg(argp,int ) ); break;
19: case 's': printf("%s", va_arg(argp,char *) ); break;
20: case 'f': printf("%f", va_arg(argp,double) ); break;
21: }
22: }
23: }
24: }
Example 4: ANSI variable-argument conventions
01: #include <varargs.h> /* UNIX */
02:
03: unix_fang( va_alist )
04: va_dcl /* NOTE: NO SEMICOLON PERMITTED HERE */
05: {
06: char *format;
07: va_list argp;
08: va_start( argp );
09:
10: for( format = va_arg(argp,char*); *format ; format++ )
11: {
12: if( *format != '%' )
13: putchar( *format );
14: else
15: {
16: switch( *++format )
17: {
18: case 'c': printf("%c", va_arg(argp,int )); break;
19: case 'd': printf("%d", va_arg(argp,int )); break;
20: case 's': printf("%s", va_arg(argp,char *)); break;
21: case 'f': printf("%f", va_arg(argp,double)); break;
22: }
23: }
24: }
25: }
Example 5: Unix variable-argument conventions
298: else
299: {
if( sizeof(VTYPE) != sizeof(double) )
rval = (VTYPE) atol( Str );
else
{
rval = atof( Str );
while( isdigit(*Str) || *Str == '.' )
Str++;
if(*Str == 'E' || *Str == 'e' ) /* 12.34E+03 */
Str += 2 ;
}
while( isdigit(*Str) )
Str++;
307: }
Example 6: Corrections to code in C Chest, Listing Six, February 1987